package com.fjc.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fjc.dao.SysRoleMapper;
import com.fjc.dao.SysRolePermissionMapper;
import com.fjc.domain.ResultObject;
import com.fjc.domain.SysRole;
import com.fjc.domain.SysRolePermission;
import com.fjc.domain.SysRolePermissionExample;
import com.fjc.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SysRoleServiceImpl implements SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Resource
    private SysRolePermissionMapper SysRolePermissionMapper;

    @Override
    public ResultObject getRoles(Integer page, Integer limit) {
        if(page > 0){limit = page * limit;}
        List<SysRole> list = sysRoleMapper.selectAllLimit(page,limit);
        return new ResultObject(0,"成功",list,list.size());
    }

    @Override
    public ResultObject delRole(Integer id) {
        sysRoleMapper.deleteByPrimaryKey(id);
        return new ResultObject(0,"成功");
    }

    @Override
    public ResultObject saveRole(SysRole sysRole) {
        if(sysRole.getId() == null){
            //新增操作
            sysRoleMapper.insertSelective(sysRole);
        }else{
            //更新操作
            sysRoleMapper.updateByPrimaryKeySelective(sysRole);
        }
        return new ResultObject(0,"成功");
    }

    @Override
    public ResultObject getRolePermissions(Integer id) {
        //获取一级菜单
        List<Map> list = SysRolePermissionMapper.selectParentMenuByRoleid(id);
        //获取每个一级菜单下的二级菜单
        for (Map map:list){
            List<Map> childrenList = SysRolePermissionMapper.selectChildrenMenuByRoleid(id,(int)map.get("pid"));
            if(childrenList.size() > 0){
                map.put("children",childrenList);
            }
        }

        List<Map> idMapList = SysRolePermissionMapper.selectNoDelPermissionByRoleid(id);
        List<Integer> idList = new ArrayList<>();
        for(Map map: idMapList){
            if(map.get("parent_id") != null) {
                idList.add((int)map.get("parent_id"));
            }
        }

        List<Integer> checkNode = new ArrayList<>();
        if(idList.size() > 0){
            for(Map map: idMapList){
                for(Integer pid : idList){
                    if((int)map.get("permission_id") != pid) {
                        checkNode.add((int)map.get("id"));
                    }
                }
            }
        }else{
            for(Map map: idMapList){
                checkNode.add((int)map.get("id"));
            }
        }

        Map<String ,Object> returnMap = new HashMap<>();
        returnMap.put("treeData",list);
        returnMap.put("checkNode",checkNode);
        return new ResultObject(0,"",returnMap);
    }

    @Override
    public ResultObject updatePermissionsByRoleId(JSONObject object) {
        JSONArray checkNodes = object.getJSONArray("keys");
        Integer roleid = object.getInteger("roleid");

        //获取该用户所有权限id
        List<Integer> allPermissionIds = SysRolePermissionMapper.selectAllPermissionByRoleid(roleid);

        List<Integer> checkedPermissinsIds = checkNodes.toJavaList(Integer.class);
        for(Integer id: checkedPermissinsIds){
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(id);
            sysRolePermission.setStatus(1);
            SysRolePermissionMapper.updateByPrimaryKeySelective(sysRolePermission);
        }

        //与现在选择的权限id比较，生成已经取消的权限id
        allPermissionIds.removeAll(checkedPermissinsIds);

        //更新取消的权限id
        for(Integer id:allPermissionIds){
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(id);
            sysRolePermission.setStatus(0);
            SysRolePermissionMapper.updateByPrimaryKeySelective(sysRolePermission);
        }

        //更新有父级的权限
        for(int j=0;j< checkNodes.size();j++){
            List<Map> hasParent = SysRolePermissionMapper.selectParentIsNotNull(checkNodes.getInteger(j));
            if(hasParent.size() > 0){
                for(Map map : hasParent){
                    SysRolePermission sysRolePermission = new SysRolePermission();
                    sysRolePermission.setStatus(1);
                    SysRolePermissionExample sysRolePermissionExample = new SysRolePermissionExample();
                    SysRolePermissionExample.Criteria criteria = sysRolePermissionExample.createCriteria();
                    criteria.andPermissionIdEqualTo((int)map.get("parent_id"));
                    SysRolePermissionMapper.updateByExampleSelective(sysRolePermission,sysRolePermissionExample);
                }
            }
        }

        return new ResultObject(0,"成功");
    }
}
